<?php

namespace app\index\controller;

use think\Controller;

use app\common\controller\SmallBase;

use think\Db;

use app\index\controller\WxUser;

class Base extends Controller
{
    public  $uid;
    public $userInfo;
    public $userAttribute;
    public $userReal;
    protected $user_model = null;

    public function _initialize()
    {
        $this->user_model = new \app\common\model\User;
        $this->firstBlock();
    }
    //验证客户信息
    public function firstBlock(){

        $user_info_wx = session("userinfo");
        $user_id = 0;
        //file_put_contents("openid.txt", "openid" . json_encode($user_info_wx) . PHP_EOL, FILE_APPEND);
        if($user_info_wx){
            $user_info_wx = Db::name("wx_users")->where("openid",$user_info_wx["openid"])->find();//更新数据库的wx用户
            $user_id = $user_info_wx["uid"];//获取用户id
            $user_info_bv = Db::name("user")->where("uid",$user_id)->find();
            if(!$user_info_bv){
                $user_id = 0;
            }
        }
        //echo $user_id;exit;
        $parent_id = input("parent_id");//父id
        $lock = input("lock");//锁
//        dump(input());
//
//        echo "aaa";exit;
        //file_put_contents( "first_block.txt", "邀请parent_id2".$parent_id.PHP_EOL, FILE_APPEND);
        $wxUser = new WxUser();
        if($user_id){
            $real_status = Db::name("user_real")->where("uid",$user_id)->value("real_status");//获取认证的状态

            //验证通过获取用户信息
            $this->userInfo = Db::name("user")->where("uid",$user_id)->find();//用户信息
            $this->uid =  $user_id ;//用户id
            $this->userAttribute = Db::name("user_attribute")->where("uid",$user_id)->find();//获取派克属性表
            $this->userReal = Db::name("user_real")->where("uid",$user_id)->find();//获取派克认证信息表

            if($this->userInfo["black"]==1){

                $this->redirect("Func/forbidden");

            }

            $controller = request()->controller();
            $action = request()->action();
            //控制器和第二次方法的时候返回不需要进行实名认证
            if(strtolower($controller)=="real" && strtolower($action)=="apply_act"){
                return;
            }else {
                //认证不通过的需要实名认证
                if ($real_status != 2) {
                    $wxUser->wxTowxInfo($parent_id,$lock);
                }

            }
        }else{
            //echo $lock."</br>";exit;
                $wxUser->wxTowxInfo($parent_id,$lock);//没有uid,需要实名认证检查有没有微信
        }

    }


    //是否注册
//    public function isRegister(){
//        $uid = $this->uid;
//        if($uid && !empty($uid)){
//            $user_info = $this->user_model->with("userAttribute")->where("uid",$uid)->find();
//            if($user_info){
//                if($user_info["status"]==0){
//                    renderJson("该账户被封禁",0);
//                }
//                if($user_info["black"]==1){
//                    renderJson("该账户列入黑名单",0);
//                }
//                if($user_info["userAttribute"]->account_status==2){
//                    renderJson("该账户被禁用",0);
//                }
//                $this->userAttribute = $user_info["userAttribute"];
//                unset($user_info["userAttribute"]);
//                $this->userInfo = $user_info;
//            }else{
//                return $this->fetch("shop/register");
//            }
//        }else{
//            return $this->fetch("shop/register");
//        }
//
//
//    }
    //是否认证
//    public function isAuther(){
//        $uid = $this->uid;
//        $real_info = Db::name("user_real")->where("uid",$uid)->find();
//        if($real_info){
//            if($real_info["real_status"]==1 || $real_info["real_status"]==3){
//                return $this->fetch("shop/first");
//            }
//            if($real_info["real_status"]==2){
//                $this->userReal = $real_info;
//            }
//        }
//
//
//
//
//    }



}